package com.example.petshopping.ui.category.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.category.model.SubCategory;

import java.util.List;

public class SubCategoryAdapter extends RecyclerView.Adapter<SubCategoryAdapter.SubCategoryViewHolder> {
    
    private List<SubCategory> subCategories;
    private OnSubCategoryClickListener listener;
    
    public interface OnSubCategoryClickListener {
        void onSubCategoryClick(SubCategory subCategory);
    }
    
    public SubCategoryAdapter(List<SubCategory> subCategories, OnSubCategoryClickListener listener) {
        this.subCategories = subCategories;
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public SubCategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_subcategory, parent, false);
        return new SubCategoryViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull SubCategoryViewHolder holder, int position) {
        SubCategory subCategory = subCategories.get(position);
        holder.tvSubCategoryName.setText(subCategory.getName());
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onSubCategoryClick(subCategory);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return subCategories.size();
    }
    
    public void updateData(List<SubCategory> subCategories) {
        this.subCategories = subCategories;
        notifyDataSetChanged();
    }
    
    static class SubCategoryViewHolder extends RecyclerView.ViewHolder {
        TextView tvSubCategoryName;
        
        public SubCategoryViewHolder(@NonNull View itemView) {
            super(itemView);
            tvSubCategoryName = itemView.findViewById(R.id.tv_subcategory_name);
        }
    }
} 